#include "fig_enemy_pathcraft.h"
#include "fighter.h"
#include "fig_flyingpath.h"
#include "fig_player_craft.h"
#include "fig_pickup_manager.h"
#include "fig_enemybullet_master.h"

#include "dd/dd_man.h"
#include "math/facing.h"

#include "num/rand.h"
#include "application/game_script.h"
#include "console/console.h"
#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "dd/dd_man.h"
#include "font/font_manager.h"
#include "script/scripthelper.h"
#include "input/keyboard.h"
#include "graphics/graphics_util.h"
#include "luaplus/LuaObject.h"
#include "script/scripthelper.h"
#include "script/write_lua.h"
#include "lang/lang.h"
#include "vfs/vfs.h"
#include "script/file_list.h"

#include "font/font_manager.h"
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging
#include "util/timer.h"		    // logging


FIGEnemyPathCraft::FIGEnemyPathCraft(FIGEnemyCraft::ENEMY_TYPES EnemyType) : 
										FIGEnemy3DRotate(EnemyType),		
										m_IndexOfGroup(0),
										m_PathType(FIGFlyingPath::PATH_NONE),
										m_bIsDying(false)
{
	m_Pos = math::Vec2( 0.5f,0.5f);	
	m_Rotation = 360;
	m_MoveAbitCounter.SetLimit(0.9f);
	m_MoveAbitCounter.ForceReady();
	// simple hit test with circle
	// instead of full sprite check
	m_IsHitTestCircle = true;			
	m_ShootCounter.SetLimit(num::RAND32::Instance().RandFloat(1.0f,2.0f));
	m_Speed = 0.3f;

}


void		FIGEnemyPathCraft::Tick(float dt)
{
	

	FIGEnemy3DRotate::Tick(dt);

	if(m_DelaySpawn > 0)
		return;

	if(!m_IsAlive && !m_bIsDying)
		return;

	if(m_Life <= 0 && !m_bIsDying)
		return;

	const FIGFlyingPath::PathInfo PathInfo = 
							FIGFlyingPath::Instance().GetPosFromPath(
																	m_PathType,
																	m_CraftInfo,
																	dt);
	
	if(PathInfo.m_IsDone == false)
	{
		m_Rotation = m_Rotation + ( PathInfo.Rotation);
		
		//if(m_CraftInfo.RotationDone < 0)
		//	m_CraftInfo.RotationDone *= -1;
		
		math::Vec2 Dir = math::Facing::VectorFromAngle(m_Rotation-180);
		Dir.Normalize();
		float Dist = dt * m_Speed;
		m_Pos += ( Dir * Dist);
		m_CraftInfo.DistantTravelled += Dist;
		
	}
	else
	{
		m_IsAlive = false;
	}
		
	
}


void		FIGEnemyPathCraft::SetFlyPathInfo(const FIGLevel::EnemyInfo* pEnemyInfo,
											  int IndexOfGroup)
{
	m_PathType =(FIGFlyingPath::PATH_TYPE) pEnemyInfo->FlyingPathEnumInt;
	m_IndexOfGroup = IndexOfGroup;
	m_DelaySpawn = pEnemyInfo->DelaySpawn * m_IndexOfGroup;
	
}

void		FIGEnemyPathCraft::ReSpawn()
{	
	FIGEnemy3DRotate::ReSpawn();
	
	FIGFlyingPath::FlyingPath* pPath = FIGFlyingPath::Instance().GetPath(this);
	if(pPath)
	{
		m_Pos = pPath->m_StartPos;
		m_Rotation = pPath->m_InitialRotation; 			
	}

	if( m_PathType == FIGFlyingPath::PATH_LEFT_TO_RIGHT_DIAGONAL ||
		m_PathType == FIGFlyingPath::PATH_RIGHT_TO_LEFT_DIAGONAL 
		)
	{
		m_Pos.y += ( 0.1f * m_IndexOfGroup );
	}

	m_CraftInfo.Reset();
	m_IsReadyToFire = true;
	m_ShootCounter.SetLimit(num::RAND32::Instance().RandFloat(1.0f,2.0f));

	
}



FIGFlyingPath::PATH_TYPE			FIGEnemyPathCraft::GetPathType()
{
	return m_PathType;
	
	//return NULL;
}

void		FIGEnemyPathCraft::OnShoot()
{
	
	if(num::RAND32::Instance().RandInt(0,1) == 1)
	{
		// sometime skip shooting
		FIGEnemyBulletMaster::Instance().Shoot(
												this,
												math::Vec2(0,0) , 
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_ROTATE, 
												FIGBullet::SHOOT_STYLES_SINGLE, 
												m_Rotation,
												0,
												true,
												NULL
												);
	}

	m_ShootCounter.SetLimit(num::RAND32::Instance().RandFloat(1.5f,5.0f));
	//m_IsReadyToFire = false;
}



